<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
<!--      <el-form-item label="变更单号" prop="changeCode">
        <el-input
          v-model="queryParams.changeCode"
          placeholder="请输入变更单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="销售单号" prop="soNumber">
        <el-input
          v-model="queryParams.soNumber"
          placeholder="请输入销售订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="生产工单ID" prop="workorderId">
        <el-input
          v-model="queryParams.workorderId"
          placeholder="请输入生产工单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="生产工单号" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入生产工单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="生产任务ID" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入生产任务ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="生产任务号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入生产任务号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="产品ID" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="产品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="变更时间" prop="changeDate">
        <el-date-picker clearable
          v-model="queryParams.changeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择变更时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="变更人" prop="changePerson">
        <el-input
          v-model="queryParams.changePerson"
          placeholder="请输入变更人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="备用字段1" prop="attr1">
        <el-input
          v-model="queryParams.attr1"
          placeholder="请输入备用字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="备用字段2" prop="attr2">
        <el-input
          v-model="queryParams.attr2"
          placeholder="请输入备用字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="备用字段3" prop="attr3">
        <el-input
          v-model="queryParams.attr3"
          placeholder="请输入备用字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="备用字段4" prop="attr4">
        <el-input
          v-model="queryParams.attr4"
          placeholder="请输入备用字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:drawingChange:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:drawingChange:edit']"
        >修改</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:drawingChange:remove']"
        >删除</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:drawingChange:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drawingChangeList" @selection-change="handleSelectionChange" border>
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="变更单号" align="center" prop="changeCode">
        <template slot-scope="scope">
          <el-button
              type="text"
              size="small"
              @click="handleView(scope.row)"
              v-hasPermi="['md:drawingChange:query']"
          >{{ scope.row.changeCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="销售订单编号" align="center" prop="soNumber" />
<!--      <el-table-column label="生产工单ID" align="center" prop="workorderId" />-->
      <el-table-column label="生产工单号" align="center" prop="workorderCode" />
<!--      <el-table-column label="生产任务ID" align="center" prop="taskId" />-->
      <el-table-column label="生产任务号" align="center" prop="taskCode" />
<!--      <el-table-column label="产品ID" align="center" prop="itemId" />-->
      <el-table-column label="产品编码" align="center" prop="itemCode" />
      <el-table-column label="产品名称" align="center" prop="itemName" />
      <el-table-column label="变更时间" align="center" prop="changeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.changeDate, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="变更人" align="center" prop="changePerson" />
<!--      <el-table-column label="备用字段1" align="center" prop="attr1" />-->
<!--      <el-table-column label="备用字段2" align="center" prop="attr2" />-->
<!--      <el-table-column label="备用字段3" align="center" prop="attr3" />-->
<!--      <el-table-column label="备用字段4" align="center" prop="attr4" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:drawingChange:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:drawingChange:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图纸变更对话框 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        width="1200px"
        :close-on-click-modal="false"
        :before-close="beforeDialogClose"
        append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-row>
          <el-form-item label="ID" prop="id" style="display: none;">
            <el-input v-model="form.id" placeholder="请输入生产任务ID" />
          </el-form-item>
          <el-col :span="8">
            <el-form-item label="变更单号" prop="changeCode">
              <el-input v-model="form.changeCode" placeholder="请输入变更单号" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="80">
              <el-switch v-model="autoGenFlag"
                         active-color="#13ce66"
                         active-text="自动生成"
                         @change="handleAutoGenChange(autoGenFlag)"
                         v-if="form.id === null"
                         >
              </el-switch>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="生产任务ID" prop="taskId" style="display: none;">
              <el-input v-model="form.taskId" placeholder="请输入生产任务ID" />
            </el-form-item>
            <el-form-item label="生产任务号" prop="taskCode">
              <el-input v-model="form.taskCode" placeholder="请输入生产任务号" readonly>
                <el-button
                    slot="append"
                    @click="handleSelectTask"
                    icon="el-icon-search"
                    v-if="form.id === null"
                >
                </el-button>
              </el-input>
            </el-form-item>
            <TaskSelect ref="taskSelect" @onSelected="onTaskSelected" > </TaskSelect>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="销售订单号" prop="soNumber">
              <el-input v-model="form.soNumber" placeholder="请输入销售订单编号" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产工单ID" prop="workorderId" style="display: none;">
              <el-input v-model="form.workorderId" placeholder="请输入生产工单ID" />
            </el-form-item>
            <el-form-item label="生产工单号" prop="workorderCode">
              <el-input v-model="form.workorderCode" placeholder="请输入生产工单号" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品ID" prop="itemId" style="display: none;">
              <el-input v-model="form.itemId" placeholder="请输入产品ID" />
            </el-form-item>
            <el-form-item label="产品编码" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请输入产品编码" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入产品名称" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="center" class="custom-divider" v-if="form.id">变更明细</el-divider>
        <ChangeLine :taskId="form.taskId" :changeId="form.id" :optType="optType" v-if="form.id"></ChangeLine>



<!--        <el-form-item label="变更时间" prop="changeDate">
          <el-date-picker clearable
            v-model="form.changeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择变更时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="变更人" prop="changePerson">
          <el-input v-model="form.changePerson" placeholder="请输入变更人" />
        </el-form-item>
        <el-form-item label="备用字段1" prop="attr1">
          <el-input v-model="form.attr1" placeholder="请输入备用字段1" />
        </el-form-item>
        <el-form-item label="备用字段2" prop="attr2">
          <el-input v-model="form.attr2" placeholder="请输入备用字段2" />
        </el-form-item>
        <el-form-item label="备用字段3" prop="attr3">
          <el-input v-model="form.attr3" placeholder="请输入备用字段3" />
        </el-form-item>
        <el-form-item label="备用字段4" prop="attr4">
          <el-input v-model="form.attr4" placeholder="请输入备用字段4" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="optType !== 'view'">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDrawingChange,
  getDrawingChange,
  delDrawingChange,
  addDrawingChange,
  updateDrawingChange,
  selectByCode
} from "@/api/mes/md/drawingChange";
import TaskSelect from "./taskSelect.vue";
import { genCode } from "@/api/system/autocode/rule";
import ChangeLine from "./line.vue";

export default {
  name: "DrawingChange",
  components: {
    TaskSelect, ChangeLine
  },
  data() {
    return {
      //自动生成编码
      autoGenFlag:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图纸变更表格数据
      drawingChangeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        changeCode: null,
        soNumber: null,
        workorderId: null,
        workorderCode: null,
        taskId: null,
        taskCode: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        changeDate: null,
        changePerson: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        changeCode: [
          {required: true, message: "变更编号不能为空", trigger: "blur"}
        ],
        taskCode: [
          {required: true, message: "生产任务号不能为空", trigger: "change"}
        ]
      },
      optType: null,
      // 控制主弹窗是否允许关闭
      allowClose: true,
      // 是否正在尝试关闭弹窗
      isClosing: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图纸变更列表 */
    getList() {
      this.loading = true;
      listDrawingChange(this.queryParams).then(response => {
        this.drawingChangeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        changeCode: null,
        soNumber: null,
        workorderId: null,
        workorderCode: null,
        taskId: null,
        taskCode: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        changeDate: null,
        changePerson: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图纸变更";
      this.optType = 'add';
      this.allowClose = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDrawingChange(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图纸变更";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      getDrawingChange(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看图纸变更";
        this.optType = "view";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDrawingChange(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.allowClose = true;
              this.getList();
            });
          } else {
            addDrawingChange(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = true;
              selectByCode(this.form.changeCode).then(response => {
                this.form = response.data;
              })
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除图纸变更编号为"' + ids + '"的数据项？').then(function() {
        return delDrawingChange(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/drawingChange/export', {
        ...this.queryParams
      }, `drawingChange_${new Date().getTime()}.xlsx`)
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag){
      if(autoGenFlag){
        genCode('DRAWING_CHANGE_CODE').then(response =>{
          this.form.changeCode = response;
        });
      }else{
        this.form.changeCode = null;
      }
    },
    // 任务选择开关
    handleSelectTask(){
      this.$refs.taskSelect.showFlag = true;
    },
    // 任务选择弹出框
    onTaskSelected(obj){
      if(obj != undefined && obj != null){
        this.form.taskId = obj.taskId;
        this.form.taskCode = obj.taskCode;
        this.form.itemCode = obj.itemCode;
        this.form.itemName = obj.itemName;
        this.form.soNumber = obj.soNumber;
        this.form.workorderId = obj.workorderId;
        this.form.workorderCode = obj.workOrderCode;
      }
    },
    // 关闭弹窗事件
    handleClickClose() {
      if (this.allowClose) {
        this.open = false;
        this.reset();
        return;
      }
      this.$modal.confirm('是否确认关闭？弹窗关闭后将不可更改！').then(() => {
        this.allowClose = true;
        this.open = false;
        this.reset();
      }).catch(() => {
        this.$modal.msgWarning("已取消");
      });
    },
    beforeDialogClose(done) {
      if (this.allowClose) {
        done(); // 允许关闭
      } else {
        this.handleClickClose(); // 自定义逻辑
      }
    },
  }
};
</script>
<style scoped>
.custom-divider ::v-deep .el-divider__text {
  background-color: #fff;
  color: #409EFF;
  padding: 4px 12px;
  border-radius: 8px;
  border: 1px solid #409EFF;
}
.custom-divider{
  background-color: #409EFF;
}
</style>
